package com.neusoft.databus.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.databus.common.dto.DictionaryDTO;
import com.neusoft.databus.common.enums.DictionaryType;
import com.neusoft.databus.common.model.Dictionary;
import com.neusoft.databus.core.converter.DictionaryConverter;
import com.neusoft.databus.core.repository.DictionaryRepository;
import com.neusoft.databus.core.service.DictionaryService;

@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Autowired
    private DictionaryConverter dictionaryConverter;

    @Override
    public List<DictionaryDTO> findByDictionaryTypeAndParentIsNotNull(final DictionaryType dictionaryType) {
        final List<Dictionary> dictionaries =
                this.dictionaryRepository.findByDictionaryTypeAndParentIsNotNull(dictionaryType);
        return this.dictionaryConverter.toListDTO(dictionaries);
    }

    @Override
    public List<DictionaryDTO> findByDictionaryTypeAndParentIsNull(final DictionaryType dictionaryType) {
        final List<Dictionary> dictionaries =
                this.dictionaryRepository.findByDictionaryTypeAndParentIsNull(dictionaryType);
        return this.dictionaryConverter.toListDTO(dictionaries);
    }

    @Override
    public DictionaryDTO save(final DictionaryDTO dto) {
        final Dictionary model = this.dictionaryConverter.toModel(dto);
        this.dictionaryRepository.save(model);
        return this.dictionaryConverter.toDTO(model);
    }

}
